package com.zxq.ruoyi.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.zxq.ruoyi.domain.IotProduct;
import com.zxq.ruoyi.domain.IotProject;
import com.zxq.ruoyi.domain.vo.ProductProjectVoInfo;
import com.zxq.ruoyi.service.impl.IotProductServiceImpl;
import com.zxq.ruoyi.service.impl.IotProjectServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zxq.ruoyi.domain.IotProjectProduct;
import com.zxq.ruoyi.service.IIotProjectProductService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 项目与产品关联Controller
 * 
 * @author ruoyi
 * @date 2022-09-14
 */
@RestController
@RequestMapping("/projectProduct")
public class IotProjectProductController extends BaseController
{
    @Autowired
    private IIotProjectProductService iotProjectProductService;

    @Autowired
    private IotProjectServiceImpl iotProjectService;

    @Autowired
    private IotProductServiceImpl iotProductService;

    /**
     * 查询项目与产品关联列表
     */
    @RequiresPermissions("iot:projectProduct:list")
    @GetMapping("/list")
    public ProductProjectVoInfo list(IotProjectProduct iotProjectProduct)
    {
        startPage();
        List<IotProjectProduct> list = iotProjectProductService.selectIotProjectProductList(iotProjectProduct);
        ProductProjectVoInfo rspData = new ProductProjectVoInfo();
        rspData.setCode(200);
        rspData.setRows(list);
        rspData.setMsg("查询成功");
        rspData.setTotal((new PageInfo(list)).getTotal());
        List<IotProject> projects = iotProjectService.selectIotProjectList(new IotProject());
        List<IotProduct> products = iotProductService.selectIotProductList(new IotProduct());
        rspData.setProductList(products);
        rspData.setProjectList(projects);

        return rspData;
    }

    /**
     * 导出项目与产品关联列表
     */
    @RequiresPermissions("iot:projectProduct:export")
    @Log(title = "项目与产品关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotProjectProduct iotProjectProduct)
    {
        List<IotProjectProduct> list = iotProjectProductService.selectIotProjectProductList(iotProjectProduct);
        ExcelUtil<IotProjectProduct> util = new ExcelUtil<IotProjectProduct>(IotProjectProduct.class);
        util.exportExcel(response, list, "项目与产品关联数据");
    }

    /**
     * 获取项目与产品关联详细信息
     */
    @RequiresPermissions("iot:projectProduct:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iotProjectProductService.selectIotProjectProductById(id));
    }

    /**
     * 新增项目与产品关联
     */
    @RequiresPermissions("iot:projectProduct:add")
    @Log(title = "项目与产品关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotProjectProduct iotProjectProduct)
    {
        return toAjax(iotProjectProductService.insertIotProjectProduct(iotProjectProduct));
    }

    /**
     * 修改项目与产品关联
     */
    @RequiresPermissions("iot:projectProduct:edit")
    @Log(title = "项目与产品关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotProjectProduct iotProjectProduct)
    {
        return toAjax(iotProjectProductService.updateIotProjectProduct(iotProjectProduct));
    }

    /**
     * 删除项目与产品关联
     */
    @RequiresPermissions("iot:projectProduct:remove")
    @Log(title = "项目与产品关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(iotProjectProductService.deleteIotProjectProductByIds(ids));
    }
}
